<?php
class Shopping_IndexController extends Zendvn_Controller_Action{
	
    //Mang tham so nhan duoc o moi action
    protected $_arrParam;

    //Duong dan cua controller
    protected $_currentController;
    
    //Duong dan cua Action chinh
    protected $_actionMain;
	
    //Thong so phan trang
    protected $_paginator = array(
                                'ItemCountPerPage' => 8,
                                'PageRange' => 4,
                                );
    protected $_namespace;


    public function init() {
        //Mang tham so nhan duoc o moi action
        $this->_arrParam    =   $this->_request->getParams();
        
        //Duong dan cua controller
        $this->_currentController   =   '/' . $this->_arrParam['module'] 
                                      . '/' . $this->_arrParam['controller'];
        
        //Duong dan cua Action chinh
        $this->_actionMain  =   $this->_currentController . '/index';
        
        $this->_paginator['currentPage'] = $this->_request->getParam('page', 1);
        $this->_arrParam['paginator'] = $this->_paginator;

        //Luu cac du lieu filter vao SESSION
        //Dat ten SESSION
        $this->_namespace  = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
        $ssFilter = new Zend_Session_Namespace($this->_namespace);
//	$ssFilter->unsetAll();
        if(empty($ssFilter->col)){
                $ssFilter->keywords = '';
        }

        $this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
		
        //Truyen ra view
        $this->view->arrParam   =   $this->_arrParam;
        $this->view->currentController  =   $this->_currentController ;
        $this->view->actionMain  =   $this->_actionMain;
        
        $template_path  =   TEMPLATE_PATH . "/public/books";
        $this->loadTemplate($template_path , 'template.ini', 'template');
    }
    
    public function indexAction() {
        $tblProduct = new Shopping_Model_Item();
        $this->view->Items = $tblProduct->listItem($this->_arrParam, array('task'=>'public-index'));
        
        // Phan trang
        $totalItem = $tblProduct->countItem($this->_arrParam, array('task'=>'public-index'));

        $paginator	= new Zendvn_Paginator();
        $this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
    }
    public function categoryAction() {
        $tblProduct = new Shopping_Model_Item();
        $this->view->Items = $tblProduct->listItem($this->_arrParam, array('task'=>'public-category'));
        
        $totalItem = $tblProduct->countItem($this->_arrParam, array('task'=>'public-category'));
        $paginator	= new Zendvn_Paginator();
        $this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
    }
    
    public function detailAction() {
        
        $tblProduct = new Shopping_Model_Item();
        $this->view->Item = $tblProduct->getItem($this->_arrParam, array('task'=>'public-detail'));
        $this->view->Title = $this->view->Item['name'] . '| Detail |' . 'Tteobokki';
        $this->view->headTitle($this->view->Title, true);
    }
    
    public function viewCartAction() {
        $yourCart = new Zend_Session_Namespace('cart');
        
        if($this->_request->isPost()){
            $itemProduct = $this->_arrParam['itemProduct'];
            
            if(count($itemProduct) > 0){
                if(($key = array_search(0, $itemProduct)) !== false) {
                    unset($itemProduct[$key]);
                }
            }
            $yourCart->cart = $itemProduct;
        }
        
        $ssInfo = $yourCart->getIterator();
        $this->_arrParam['cart'] = $ssInfo['cart'];
        $tblProduct = new Shopping_Model_Item();
        $this->view->Items = $tblProduct->listItem($this->_arrParam , array('task'=>'view-cart'));
        $this->view->cart = $ssInfo['cart'];
    }
    
    public function addItemAction() {
        
        $yourCart = new Zend_Session_Namespace('cart');
//        $yourCart->unsetAll();
        
        $ssInfo = $yourCart->getIterator();

        $filter = new Zend_Filter_Digits();
        $id = $filter->filter($this->_request->getParam('id'));

        if(count($yourCart->cart) == 0){
            $cart[$id] = 1;
            $yourCart->cart = $cart;
        }else{
            $tmp = $ssInfo['cart'];
            if(array_key_exists($id,$tmp) == true){
                    $tmp[$id] = $tmp[$id] + 1;
            }else{
                    $tmp[$id] = 1;
            }
            $yourCart->cart = $tmp;
        }
        echo '<pre>';
        print_r($yourCart->getIterator());
        echo '</pre>';
        
        $this->_redirect($this->_currentController . '/view-cart');
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function orderAction() {
        $yourCart = new Zend_Session_Namespace('cart');
        $ssInfo = $yourCart->getIterator();
        $this->_arrParam['cart'] = $ssInfo['cart'];
        $tblProduct = new Shopping_Model_Item();
        $this->view->Items = $tblProduct->listItem($this->_arrParam , array('task'=>'view-cart'));
        $this->view->cart = $ssInfo['cart'];
        
        if($this->_request->isPost()){
            $tblInvoice = new Shopping_Model_Invoice();
            $invoice_id = $tblInvoice->saveItem($this->_arrParam, array('task'=>'public-order'));
            $this->_arrParam['invoice_id'] = $invoice_id;
            
            $tblInvoiceDetail = new Shopping_Model_InvoiceDetail();
            $tblInvoiceDetail->saveItem($this->_arrParam, array('task'=>'public-order'));
            $yourCart->unsetAll();
        }
    }
}